<?php
class Tool {
	
	
	   public static  function getOrderNo(){
	   	
	   	return 'D'.date('YmdHis',Time () + 3600 * 6).rand(1,9999);
	   }
	
	// 获取 request
	public static function getrequestValue($name) {
		return Yii::app ()->request->getParam ( $name );
	}
	public static function getOrderByType($type, $ob) {
		$id = Yii::app ()->session ['user']->id;
		$criteria = new CDbCriteria ();
		$criteria->order = 'createtime desc ';
		$criteria->addCondition ( "peddlery_id=" . $id );
		$criteria->addCondition ( "status=" . $type );
		$count = UserOrder::model ()->count ( $criteria );
		$pager = new CPagination ( $count );
		$pager->pageSize = 8;
		$pager->applyLimit ( $criteria );
		$ordertList = UserOrder::model ()->findAll ( $criteria );
		
		$ob->render ( 'Order', array (
				'ordertList' => $ordertList,
				'pages' => $pager,
				'active' => $type 
		) );
	}
	public static function isMobile() {
		// 如果有HTTP_X_WAP_PROFILE则一定是移动设备
		if (isset ( $_SERVER ['HTTP_X_WAP_PROFILE'] )) {
			return true;
		}
		// 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
		if (isset ( $_SERVER ['HTTP_VIA'] )) {
			// 找不到为flase,否则为true
			return stristr ( $_SERVER ['HTTP_VIA'], "wap" ) ? true : false;
		}
		// 遍历法，判断手机发送的客户端标志,兼容性有待提高
		if (isset ( $_SERVER ['HTTP_USER_AGENT'] )) {
			$clientkeywords = array (
					'iphone',
					'ipad',
					'android',
					'nokia',
					'sony',
					'ericsson',
					'mot',
					'samsung',
					'htc',
					'sgh',
					'lg',
					'sharp',
					'sie-',
					'philips',
					'panasonic',
					'alcatel',
					'lenovo',
					'blackberry',
					'meizu',
					'netfront',
					'symbian',
					'ucweb',
					'windowsce',
					'palm',
					'operamini',
					'operamobi',
					'openwave',
					'nexusone',
					'cldc',
					'midp',
					'wap',
					'mobile' 
			);
			// 从HTTP_USER_AGENT中查找手机浏览器的关键字
			// if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
			// {
			// return true;
			// }
			
			// 对移动端判断更快
			$user_agent = strtolower ( $_SERVER ['HTTP_USER_AGENT'] );
			foreach ( $clientkeywords as $device ) {
				if (stristr ( $user_agent, $device )) {
					return true;
				}
			}
		}
		// 协议法，因为有可能不准确，放到最后判断
		if (isset ( $_SERVER ['HTTP_ACCEPT'] )) {
			// 如果只支持wml并且不支持html那一定是移动设备
			// 如果支持wml和html但是wml在html之前则是移动设备
			if ((strpos ( $_SERVER ['HTTP_ACCEPT'], 'vnd.wap.wml' ) !== false) && (strpos ( $_SERVER ['HTTP_ACCEPT'], 'text/html' ) === false || (strpos ( $_SERVER ['HTTP_ACCEPT'], 'vnd.wap.wml' ) < strpos ( $_SERVER ['HTTP_ACCEPT'], 'text/html' )))) {
				return true;
			}
		}
		return false;
	}
	
	public static function saveModeldata($modeldata,$mid) {

		$pid = Yii::app ()->session ['user']->id;
		
		$mode = new ModelData ();
		$mode->model_id=$mid;
		switch (trim ( $modeldata ['model'] )) {
			
			case 'config':

				$mode->model_id= $modeldata ['config_id'];
				break;
			case 'image' :
				
			
				// todo
				$mode->model = 'image';
				$mode->image = $modeldata ['image'];
				$mode->link = $modeldata ['link'];
				
				break;
			
			case 'text_nav' :
				$mode->model = 'text_nav';
				$mode->title = $modeldata ['title'];
				$mode->link = $modeldata ['link'];
				break;
			
			case 'goods_three' :
				$mode->model = 'goods_three';
				$mode->goods_three_1_link= $modeldata ['goods_three_1_link'];
				$mode->goods_three_2_link= $modeldata ['goods_three_2_link'];
				$mode->goods_three_3_link= $modeldata ['goods_three_3_link'];
				
				$mode->goods_three_1_id= $modeldata ['goods_three_1_id'];
				$mode->goods_three_2_id= $modeldata ['goods_three_2_id'];
				$mode->goods_three_3_id= $modeldata ['goods_three_3_id'];
				break;
			
			case 'title' :
				$mode->model = 'title';
				$mode->title = $modeldata ['title'];
				break;
			
			case 'classify' :
				$mode->model = 'classify';
				$mode->classify_id = $modeldata ['classifyid'];
				$mode->classify_name = $modeldata ['classifyname'];
				break;
			
			case 'four_nav' :
				
				$mode->model = 'four_nav';
				$mode->four_nav_image_1 = $modeldata ['four_nav_image_1'];
				$mode->four_nav_image_2 = $modeldata ['four_nav_image_2'];
				$mode->four_nav_image_3 = $modeldata ['four_nav_image_3'];
				$mode->four_nav_image_4 = $modeldata ['four_nav_image_4'];
				
				$mode->four_nav_link_1 = $modeldata ['four_nav_link_1'];
				$mode->four_nav_link_2 = $modeldata ['four_nav_link_2'];
				$mode->four_nav_link_3 = $modeldata ['four_nav_link_3'];
				$mode->four_nav_link_4 = $modeldata ['four_nav_link_4'];
				break;
				
				
				case 'head' :
				
					$mode->model = 'head';
					$mode->image = $modeldata ['image'];
					
					break;
					

					case 'image_carousel' :
					
			    $mode->model = 'head';
				$mode->model = 'image_carousel';
				$mode->four_nav_image_1 = $modeldata ['four_nav_image_1'];
				$mode->four_nav_image_2 = $modeldata ['four_nav_image_2'];
				$mode->four_nav_image_3 = $modeldata ['four_nav_image_3'];
				$mode->four_nav_image_4 = $modeldata ['four_nav_image_4'];
				
				$mode->four_nav_link_1 = $modeldata ['four_nav_link_1'];
				$mode->four_nav_link_2 = $modeldata ['four_nav_link_2'];
				$mode->four_nav_link_3 = $modeldata ['four_nav_link_3'];
				$mode->four_nav_link_4 = $modeldata ['four_nav_link_4'];
						break;
				
		}
		$mode->createtime = time ();
		$mode->peddlery_id = $pid;
		return $mode->save();
	}
	 
	public static function sendSMS($phone, $content) {
		$content = $content . ' 【立即购】';
		
		$conn = Yii::app ()->db_sms;
		$sql = "INSERT INTO out_mt (mobile, content, ext) VALUES(:phone,:content,'')";
		$command = $conn->createCommand ( $sql );
		$command->bindParam ( ":phone", $phone, PDO::PARAM_STR );
		$command->bindParam ( ":content", $content, PDO::PARAM_STR );	
		$command->execute ();
		
		return true;
	}
	
	public static function isPhoneNo($mobile) {
		if(!preg_match('/^1[3458][0-9]{9}$/', $mobile)){
			return false;
		} 
	
		return true;
	}
	
	public  static function  getCommodityByclassify($classifyid,$pid){
		
		$data = array();
		if(!empty($classifyid)){
			$commdity=null;
			$commodityClassifys= CommodityClassify::model()->findAll('classify_id=?',array($classifyid));
			foreach ($commodityClassifys as $cf){
				//通过关联商品的commdityID 查询处商品
				$commdity= Commodity::model ()->find('id=? and is_sell is null and peddlery_id=?',array($cf->commodity_id,$pid));
				if(!empty($commdity))
					array_push($data, $commdity);
			}
		
		}
		
		return $data;
	}
	
	public static function  getlike($value){
		
		return  "'%".$value."%'";
		
	}
	
	public  static function  getMenu($pid){
		
		$criteria = new CDbCriteria ();
		$criteria->order = 'menuindex desc ';
		$criteria->addCondition ( 'userid='.$pid );
		
		$diyList = Diymenu::model ()->findAll ( $criteria );
		
		return $diyList;
		
	}
	
	/**
	 * type 0: 加一件  1:反之
	 * classid  分类ID
	 */
	public static function updateClassifyGoodsNum($type,$classid){

		$class= Classify::model()->findbypk($classid);
		switch ($type) {
			case 0:
				$class->commodity_num=$class->commodity_num+1;
			break;
			case 1:
				$class->commodity_num=$class->commodity_num-1;
				break;
			
			default:
				;
			break;
		}
		$class->save();
		
	}

	
	
	public static function getOrderCountByType($type) {
		$id = Yii::app ()->session ['user']->id;
		$criteria = new CDbCriteria ();
		$criteria->addCondition ( "user_id=" . $id );
		$criteria->addCondition ( "status=" . $type );
		$count = UserOrder::model ()->count ( $criteria );
		
		return $count;
	}
	
}